﻿using System.Drawing;
using Microsoft.VisualBasic.PowerPacks;
using G.Runtime;

namespace GameDesigner.Source.Widgets
{
    class BoundVariableWidget : Widget
    {
        private readonly GameValue _BoundVariable;
        private readonly SimpleShape _Shape;
        private readonly GameValue _GameValue;
        
        private string _DisplayableValue;
        private string _DisplayableName;
        private Point _Point = new Point(10, 10);

        public BoundVariableWidget(GameValue var, SimpleShape control, string name = null)
        {
            _BoundVariable = var;
            _GameValue = var;
            _Shape = control;
            _DisplayableName = ((name == null) ? var.Def.Name : name);
            var.ChangedEvent += this.OnVariableChanged;
            var.FireChangedEvent();
            _DisplayableValue = _DisplayableName + ": " + var.DisplayableValue;
        }
        
        public void Dispose() 
        {
            _GameValue.ChangedEvent -= this.OnVariableChanged;
        }

        public Point Point
        {
            get { return _Point; }
            set { _Point = value; }
        }

        public string Name
        {
            get { return _DisplayableName; }
        }
        
        private void OnVariableChanged(GameValue var)
        {
            _DisplayableValue = _DisplayableName + ": " + var.DisplayableValue;
            _Shape.Refresh();
        }

        public void Update(Graphics graphics) 
        {
            Font fn = new Font("Comic Sans MS", 12);
            SolidBrush solidBlack = new SolidBrush(Color.Black);
            Point point = new Point(_Shape.Location.X + _Point.X, _Shape.Location.Y + _Point.Y); 
            graphics.DrawString(_DisplayableValue, fn, solidBlack, point);
        }

    }
}
